package com.ruoyi.center.demo.convert;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.ruoyi.center.demo.api.dto.DemoDto;
import com.ruoyi.center.demo.domain.DemoDo;
import com.ruoyi.common.core.wrapper.BaseWrapper;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
/**
 * 对象转换类
 *
 * @author ruoyi
 * @date 2022-02-15 10:59:29
 */
public class DemoConverter {

    public static LambdaQueryWrapper<DemoDo> buildWrapper(DemoDto demoDto) {
        if (demoDto == null) {
            return new LambdaQueryWrapper<>();
        }
        LambdaQueryWrapper<DemoDo> lambdaQueryWrapper = new LambdaQueryWrapper<>(DemoDo.class);
        BaseWrapper<DemoDo> baseWrapper = new BaseWrapper<>();
        baseWrapper.buildBaseWrapper(lambdaQueryWrapper, demoDto);
        if(null != demoDto.getId()){
            lambdaQueryWrapper.eq(DemoDo::getId, demoDto.getId());
        }
        if (StringUtils.isNotBlank(demoDto.getUserCode())) {
            lambdaQueryWrapper.eq(DemoDo::getUserCode, demoDto.getUserCode());
        }
        if (StringUtils.isNotBlank(demoDto.getUserName())) {
            lambdaQueryWrapper.likeRight(DemoDo::getUserName, demoDto.getUserName());
        }
        return lambdaQueryWrapper;
    }

    public static List<DemoDto> doList2DtoList(List<DemoDo> list) {
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        return Lists.newArrayList(Iterators.transform(list.iterator(), DemoConverter::do2Dto));
    }


    public static DemoDto do2Dto(DemoDo demoDo) {
        if (demoDo == null) {
            return null;
        }
        DemoDto demoDto = new DemoDto();
        demoDto.setId(demoDo.getId());
        demoDto.setUserCode(demoDo.getUserCode());
        demoDto.setUserName(demoDo.getUserName());
        demoDto.setCreateTime(demoDo.getCreateTime());
        demoDto.setCreateBy(demoDo.getCreateBy());
        demoDto.setUpdateBy(demoDo.getUpdateBy());
        demoDto.setUpdateTime(demoDo.getUpdateTime());
        return demoDto;
    }

    public static DemoDo dto2Do(DemoDto demoDto) {
        if (demoDto == null) {
            return null;
        }
        DemoDo demoDo = new DemoDo();
        demoDo.setId(demoDto.getId());
        demoDo.setUserCode(demoDto.getUserCode());
        demoDo.setUserName(demoDto.getUserName());
        demoDo.setCreateBy(demoDto.getCreateBy());
        demoDo.setCreateTime(demoDto.getCreateTime());
        demoDo.setUpdateBy(demoDto.getUpdateBy());
        demoDo.setUpdateTime(demoDto.getUpdateTime());
        demoDo.setRemark(demoDto.getRemark());
        return demoDo;
    }

    public static List<DemoDo> dtoList2DoList(List<DemoDto> dtoList) {
        if (CollectionUtil.isEmpty(dtoList)) {
            return null;
        }
        return Lists.newArrayList(Iterators.transform(dtoList.iterator(), DemoConverter::dto2Do));
    }
}
